/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox.customization

import com.almasb.fxgl.animation.*
import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.util.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.level.text.*
import com.almasb.fxgl.ui.*
import com.windea.study.kotlin.fxgl.sample.sandbox.*
import javafx.beans.binding.*
import javafx.geometry.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.util.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class CustomPauseMenuApp : GameApplication() {
	override fun initSettings(settings: GameSettings) {
		settings.sceneFactory = object : SceneFactory() {
			override fun newPauseMenu(): PauseMenu {
				return MyPauseMenu()
			}
		}
	}
	
	override fun initGame() {
		getGameWorld().addEntityFactory(MyEntityFactory())
		
		val level = getAssetLoader().loadLevel("test_level.txt", TextLevelLoader(30, 30, '0'))
		
		getGameWorld().setLevel(level)
		
		spawn("rect", 750.0, 550.0)
	}
	
	class MyPauseMenu : PauseMenu() {
		private val animation: Animation<*>
		
		init {
			contentRoot.translateX = FXGL.getAppWidth() / 2.0 - SIZE
			contentRoot.translateY = FXGL.getAppHeight() / 2.0 - SIZE
			
			var shape = Shape.subtract(Circle(SIZE.toDouble(), SIZE.toDouble(), SIZE.toDouble()), Rectangle(0.0, SIZE.toDouble(), (SIZE * 2).toDouble(), SIZE.toDouble()))
			
			val shape2 = Shape.subtract(shape, Rectangle(0.0, 0.0, SIZE.toDouble(), SIZE.toDouble()))
			
			shape = Shape.subtract(shape, Rectangle(SIZE.toDouble(), 0.0, SIZE.toDouble(), SIZE.toDouble()))
			
			shape.strokeWidth = 2.5
			shape.strokeProperty().bind(
				Bindings.`when`(shape.hoverProperty()).then(Color.YELLOW).otherwise(Color.BLACK)
			)
			
			shape.fillProperty().bind(
				Bindings.`when`(shape.pressedProperty()).then(Color.YELLOW).otherwise(Color.color(0.1, 0.05, 0.0, 0.75))
			)
			
			shape.setOnMouseClicked { requestHide() }
			
			shape2.strokeWidth = 2.5
			shape2.strokeProperty().bind(
				Bindings.`when`(shape2.hoverProperty()).then(Color.YELLOW).otherwise(Color.BLACK)
			)
			
			shape2.fillProperty().bind(
				Bindings.`when`(shape2.pressedProperty()).then(Color.YELLOW).otherwise(Color.color(0.1, 0.05, 0.0, 0.75))
			)
			shape2.setOnMouseClicked { getGameController().exit() }
			
			val shape3 = Rectangle((SIZE * 2).toDouble(), (SIZE / 2).toDouble())
			shape3.strokeWidth = 2.5
			shape3.strokeProperty().bind(
				Bindings.`when`(shape3.hoverProperty()).then(Color.YELLOW).otherwise(Color.BLACK)
			)
			
			shape3.fillProperty().bind(
				Bindings.`when`(shape3.pressedProperty()).then(Color.YELLOW).otherwise(Color.color(0.1, 0.05, 0.0, 0.75))
			)
			
			shape3.translateY = SIZE.toDouble()
			
			val textResume = FXGL.getUIFactory().newText("RESUME", Color.WHITE, 24.0)
			textResume.font = FXGL.getUIFactory().newFont(FontType.GAME, 24.0)
			textResume.translateX = 50.0
			textResume.translateY = 100.0
			textResume.isMouseTransparent = true
			
			val textExit = FXGL.getUIFactory().newText("EXIT", Color.WHITE, 24.0)
			textExit.font = FXGL.getUIFactory().newFont(FontType.GAME, 24.0)
			textExit.translateX = 200.0
			textExit.translateY = 100.0
			textExit.isMouseTransparent = true
			
			val textOptions = FXGL.getUIFactory().newText("OPTIONS", Color.WHITE, 24.0)
			textOptions.font = FXGL.getUIFactory().newFont(FontType.GAME, 24.0)
			textOptions.translateX = 110.0
			textOptions.translateY = 195.0
			textOptions.isMouseTransparent = true
			
			contentRoot.children.addAll(shape, shape2, shape3, textResume, textExit, textOptions)
			
			contentRoot.scaleX = 0.0
			contentRoot.scaleY = 0.0
			
			animation = animationBuilder()
				.duration(Duration.seconds(0.66))
				.interpolator(Interpolators.EXPONENTIAL.EASE_OUT())
				.scale(contentRoot)
				.from(Point2D(0.0, 0.0))
				.to(Point2D(1.0, 1.0))
				.build()
		}
		
		override fun onCreate() {
			animation.onFinished = EmptyRunnable
			animation.start()
		}
		
		override fun onUpdate(tpf: Double) {
			animation.onUpdate(tpf)
		}
		
		override fun onHide() {
			if(animation.isAnimating)
				return
			
			animation.onFinished = Runnable { getGameController().popSubScene() }
			animation.startReverse()
		}
		
		companion object {
			private const val SIZE = 150
		}
	}
}


fun main(args: Array<String>) {
	GameApplication.launch(CustomPauseMenuApp::class.java, args)
}

